"""
Case Type   : 资源池化
Case Name   : 缩容中集群中节点主备倒换
Create At   : 2024.10
Owner       : chen
Description :
    1.执行缩容备二
    gs_dropnode -U USER -G GROUP -h hostlist
    2.工具交互前，备一升主
    cm_ctl switchover -A
    3.恢复环境
Expect      :
    1.成功
    2.主备倒换成功
    3.恢复环境
"""

import os
import unittest
from testcase.utils.Logger import Logger
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.ComThread import ComThread
from yat.test import Node
from yat.test import macro

class SharedStorage(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.pri_comsh = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.pri_node = Node('PrimaryDbUser')
        self.pri_root_node = Node('PrimaryRoot')
        self.sta2_node = Node('Standby2DbUser')
        self.db_host = self.sta2_node.db_host
        self.s_user = self.pri_node.ssh_user

    def test_shared_storage(self):
        text = f'''-----step1: 执行缩容备二 expect: 工具执行成功-----'''
        self.log.info(text)
        course = ComThread(self.pri_comsh.exec_dropnode, 
            args=(self.sta2_node, macro.DB_ENV_PATH, True,))
        course.setDaemon(True)
        course.start()

        text = f'''-----step2: 工具交互前，备一升主 expect: 成功-----'''
        self.log.info(text)
        res = self.pri_comsh.exec_cm_ctl("switchover -A")
        self.log.info(res)
        self.assertIn(self.constant.cm_switchover_A_success_msg, res)
        course.join()
        res = course.get_result()
        self.assertIn('is running switchover/failover', res)

    def tearDown(self):
        text = f'''-----step3: 恢复环境 expect: 成功-----'''
        self.log.info(text)
        res = self.pri_comsh.exec_cm_ctl("switchover -a")
        self.log.info(res)
        self.assertIn(self.constant.cm_switchover_success_msg, res)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')